public class MaxProfit2 {
    // 买卖股票的最大时机2
    // https://leetcode.cn/problems/best-time-to-buy-and-sell-stock-ii/
    public int maxProfit(int[] prices) {
        int len = prices.length;
        // dp[i][0]表示第i天为买入状态时候的最大利润
        // dp[i][1]表示第i天为卖出状态时候的最大利润
        int[][] dp = new int[len][2];
        dp[0][0] = -prices[0];
        for (int i = 1; i < len; i++) {
            dp[i][0] = Math.max(dp[i-1][0], dp[i-1][1] - prices[i]);
            dp[i][1] = Math.max(dp[i-1][1], dp[i-1][0] + prices[i]);
        }
        return dp[len-1][1];
    }

    // 贪心
    public int maxProfit1(int[] prices) {
        int min = 0x3f3f3f3f;
        int max = -0x3f3f3f3f;
        int ret = 0;
        for (int price : prices) {
            if (price < min) {
                min = price;
                max = price;
            }
            if (price > max) {
                max = price;
                ret += (max - min);
                min = price;
            }
        }
        return ret;
    }
}
